<?php
/*
 * 信息搜索模块
 * @package    front
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: d4408362d6b4eae688bc4eb076f2efc849a92e87 $
 */
class Search {

	private $_table;
	private $_open;
	private $_param;
	private $_module;
	private $_modSetting;
	private $_pn;
	private $_pageSize = 8;
	private $_db;


	public function __construct($open = array(), $param = array()) {
		global $db;
		$this->_open  = $open;
		$this->_param = $param;
		$this->_pn    = new Pagination();
		$this->_pn->padding(false);
		$this->_db    = &$db;
		if ($this->_getOpen('kw') && $this->_getParam('kw')) {
			$this->_checkKwLength();
			$this->_checkTimelimit();
		}
	}

	public function setPageSize($pageSize){
		$this->_pageSize = $pageSize;
	}

	public function getMainCat() {
		if ($this->_getOpen('catid')) {
			return Category::relatives($this->_module['module'], $this->_getParam('catid'));
		}
		return array();
	}

	public function getMainArea() {
		if ($this->_getOpen('areaid')) {
			$areaFilter = Area::children($this->_getParam('areaid'));
			if (!$areaFilter) {
				$areaFilter = Area::siblings($this->_getParam('areaid'));
			}
			return $areaFilter;
		} else {
			return false;
		}

	}

	public function setModule($moduleName) {
		$this->_module     = Module::info($moduleName);
		$this->_modSetting = Module::setting($moduleName);
	}

	public function getModule() {
		return $this->_module;
	}

	public function setTable($table) {
		$this->_table = $table;
	}

	public function getCondition($ext = ''){
		$condition = ' 1=1 and status=3 ';
		if ($this->_getOpen('areaid')) {
			$areaid = $this->_getParam('areaid');
			if ($areaid) {
				$area = Area::get($areaid);
				$condition .= ' and areaid in ('.$area['arrchildid'].')';
			}
		}

		if ($this->_getOpen('catid')) {
			$catid = $this->_getParam('catid');
			if ($catid) {
				$cat = Category::module($this->_module['module'], $catid);
				$condition .= ' and catid in ('.$cat['arrchildid'].')';
			}
		}
		if ($ext) {
			$condition .= $ext;
		}
		return $condition;
	}

	public function getItems($fields = '*') {
		$condition = $this->getCondition();
		$cacheSearch = Module::siteSetting('cache_search');
		$items    = $this->_db->count($this->_table, $condition, $cacheSearch);
		$this->_pn->recordsPerPage($this->_pageSize);
		$this->_pn->records($items);
		$sql = "select $fields from $this->_table" ;
		if ($condition) {
			$sql .= ' where ' . $condition;
		}
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pageSize . ',' . $this->_pageSize;
		$rs = $this->_db->query($sql);
		$list = array();
		while ($r = $this->_db->fetchArray($rs)) {
			$list[] = $r;
		}
		return $list;

	}

	public function getPn() {
		return $this->_pn;
	}

	private function _getOpen($type) {
		if (in_array($type, $this->_open)) {
			return true;
		}
		return false;
	}

	private function _getParam($param) {
		if (isset($this->_param[$param]) && $this->_param[$param]) {
			return $this->_param[$param];
		}
		return '';
	}

	private function _checkKwLength() {
		$DT = Module::siteSetting();
		include load('search.lang');
		$kw = $this->_getParam('kw');
		if (strlen($kw) < $DT['min_kw'] || strlen($kw) > $DT['max_kw']) {
			Prompt::message(lang($L['word_limit'], array($DT['min_kw'], $DT['max_kw'])), Env::referer());
		}
	}

	private function _checkTimelimit() {
		$DT      = Module::siteSetting();
		$page    = $GLOBALS['page'];
		$DT_TIME = $GLOBALS['DT_TIME'];
		if ($DT['search_limit'] && $page == 1 && getenv("HTTP_REFERER")) {
			if (($DT_TIME - $DT['search_limit']) < Cookie::get('last_search')) {
				Prompt::message(lang($L['time_limit'], array($DT['search_limit'])), Env::referer());
				Cookie::set('last_search', $DT_TIME);
			}
		}
	}
	public static function mainCat($modulename, $catid) {
		return Category::relatives($modulename, $catid);
	}

	public static function mainArea($areaid) {
		$areaFilter = Area::children($areaid);
		if (!$areaFilter) {
			$areaFilter = Area::siblings($areaid);
		}
		return $areaFilter;
	}

	public static function checkKwLength($kw){
		$DT = Module::siteSetting();
		include load('search.lang');
		if (strlen($kw) < $DT['min_kw'] || strlen($kw) > $DT['max_kw']) {
			Prompt::message(lang($L['word_limit'], array($DT['min_kw'], $DT['max_kw'])), Env::referer());
		}
	}

	public static function checkTimeLimit(){
		$DT      = Module::siteSetting();
		$page    = $GLOBALS['page'];
		$DT_TIME = $GLOBALS['DT_TIME'];
		if ($DT['search_limit'] && $page == 1 && getenv("HTTP_REFERER")) {
			if (($DT_TIME - $DT['search_limit']) < Cookie::get('last_search')) {
				Prompt::message(lang($L['time_limit'], array($DT['search_limit'])), Env::referer());
				Cookie::set('last_search', $DT_TIME);
			}
		}
	}
}